//package netty.demo2;
//
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.codec.DelimiterBasedFrameDecoder;
//import io.netty.handler.codec.FixedLengthFrameDecoder;
//import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
//import io.netty.handler.codec.LengthFieldPrepender;
//import io.netty.handler.codec.string.StringDecoder;
//
//public class TimeServer {
//
//    public static void main(String[] args) throws Exception {
//        new TimeServer().bind(8080);
//    }
//
//    public void bind(int port) throws Exception {
//        //配置服务㾍的NIO线程组
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try{
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup, workerGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .option(ChannelOption.SO_BACKLOG, 1024)
//                    .childHandler(new ChildChannelHandler());
//            //绑定端口，同步等待成功
//            ChannelFuture f = b.bind(port).sync();
//            //等待服务端监听端口关闭
//            f.channel().closeFuture().sync();
//        }finally {
//            //优雅退出，释放线程池资源
//            bossGroup.shutdownGracefully();
//            workerGroup.shutdownGracefully();
//        }
//    }
//
//    private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
//
//        @Override
//        protected void initChannel(SocketChannel ch) throws Exception {
//            ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
//            ch.pipeline().addLast(new UserDecoder(), new UserEncoder());
//            ch.pipeline().addLast(new LengthFieldPrepender(2));
//            ch.pipeline().addLast(new TimeServerHandler());
//
//
//            ch.pipeline().addLast(new StringDecoder());
//            ch.pipeline().addLast(new FixedLengthFrameDecoder(10));
//        }
//    }
//}
//
//
